package com.example.droideep.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.example.droideep.R;
import com.example.droideep.common.activities.SimpleFragmentBase;
import com.example.droideep.common.dummydata.RadioButtons;

import java.util.Set;

import butterknife.InjectView;
import droideep.library.utils.MetricsConverter;
import droideep.library.widget.FloatingActionButton;
import droideep.library.widget.ObservableScrollView;


/**
 * A simple {@link Fragment} subclass.
 */
public class FloatingActionButtonFragment extends SimpleFragmentBase {

    private static final int SEEKBAR_PROGRESS_MULTIPLIER = 10;

    @InjectView(R.id.scroll_view)
    ObservableScrollView scrollView;
    @InjectView(R.id.fab_radio_group_button_type)
    RadioGroup fabTypeRadioGroup;
    @InjectView(R.id.floating_action_button)
    FloatingActionButton floatingActionButton;
    @InjectView(R.id.fab_seek_bar_shadow_radius)
    SeekBar fabShadowRadiusSeekBar;
    @InjectView(R.id.fab_seek_bar_shadow_x_offset)
    SeekBar fabShadowXOffsetSeekBar;
    @InjectView(R.id.fab_seek_bar_shadow_y_offset)
    SeekBar fabShadowYOffsetSeekBar;
    @InjectView(R.id.fab_radio_group_fab_color)
    RadioGroup fabColorRadioGroup;

    public FloatingActionButtonFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getContentResId() {
        return R.layout.fragment_floating_action_button;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        floatingActionButton.attachToScrollView(scrollView);
        floatingActionButton.setShowAnimation(FloatingActionButton.Animations.SCALE_UP);
        floatingActionButton.setHideAnimation(FloatingActionButton.Animations.SCALE_DOWN);

        initFabTypeRadioGroup();
        initFabShadowRadiusSeekBar();
        initFabShadowXOffsetSeekBar();
        initFabShadowYOffsetSeekBar();
        initFabColorRadioGroup();
    }

    private void initFabTypeRadioGroup() {
        switch (floatingActionButton.getType()) {
            case NORMAL:
                fabTypeRadioGroup.check(R.id.fab_radio_button_normal);
                break;
            case MINI:
                fabTypeRadioGroup.check(R.id.fab_radio_button_mini);
                break;
        }
        fabTypeRadioGroup.setOnCheckedChangeListener(new FabTypeChangeListener());
    }

    private void initFabShadowRadiusSeekBar() {
        fabShadowRadiusSeekBar.setProgress((int) MetricsConverter.pxToDp(getActivity(), floatingActionButton.getShadowRadius() * SEEKBAR_PROGRESS_MULTIPLIER));
        fabShadowRadiusSeekBar.setOnSeekBarChangeListener(new ShadowRadiusChangeListener());
    }

    private void initFabShadowXOffsetSeekBar() {
        final int progress = (int) (MetricsConverter.pxToDp(getActivity(), floatingActionButton.getShadowXOffset()) * SEEKBAR_PROGRESS_MULTIPLIER +
                fabShadowXOffsetSeekBar.getMax() / 2);
        fabShadowXOffsetSeekBar.setProgress(progress);
        fabShadowXOffsetSeekBar.setOnSeekBarChangeListener(new ShadowOffsetChangeListener());
    }

    private void initFabShadowYOffsetSeekBar() {
        final int progress = (int) (MetricsConverter.pxToDp(getActivity(), floatingActionButton.getShadowYOffset()) * SEEKBAR_PROGRESS_MULTIPLIER +
                fabShadowYOffsetSeekBar.getMax() / 2);
        fabShadowYOffsetSeekBar.setProgress(progress);
        fabShadowYOffsetSeekBar.setOnSeekBarChangeListener(new ShadowOffsetChangeListener());
    }

    private void initFabColorRadioGroup() {
        populateColorRadioGroup(fabColorRadioGroup, RadioButtons.COLORS);
    }

    private void populateColorRadioGroup(RadioGroup group, Set<RadioButtons.ColorsInfo> colorsInfos) {
        for (RadioButtons.ColorsInfo colorsInfo : colorsInfos) {
            final int color = getResources().getColor(colorsInfo.primaryColorResId);
            final int textColor = color == getResources().getColor(R.color.fab_material_white) ?
                    getResources().getColor(R.color.fab_material_black) : color;
            RadioButton radioButton = new RadioButton(getActivity());
            radioButton.setText(getString(colorsInfo.colorTextResId));
            radioButton.setTextColor(textColor);
            radioButton.setTag(colorsInfo);
            group.addView(radioButton);
        }
    }

    private class FabTypeChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (fabTypeRadioGroup.equals(group)) {
                switch (checkedId) {
                    case R.id.fab_radio_button_normal:
                        floatingActionButton.setType(FloatingActionButton.Type.NORMAL);
                        break;
                    case R.id.fab_radio_button_mini:
                        floatingActionButton.setType(FloatingActionButton.Type.MINI);
                        break;
                }
            }
        }
    }

    private class ShadowRadiusChangeListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fabShadowRadiusSeekBar.equals(seekBar)) {
                floatingActionButton.setShadowRadius(getSeekBarRealProgress(progress));
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
//            Snackbar.with(getActivity())
//                    .text(String.format("Fab Shadow Radius:%s,SeekBar Progress:%s",
//                            getSeekBarRealProgress(seekBar.getProgress()), seekBar.getProgress()))
//                    .show(getActivity());
        }
    }

    protected static float getSeekBarRealProgress(int progress) {
        return (float) progress / SEEKBAR_PROGRESS_MULTIPLIER;
    }

    private class ShadowOffsetChangeListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fabShadowXOffsetSeekBar.equals(seekBar)) {
                floatingActionButton.setShadowXOffset(getRealProgress(progress, seekBar.getMax()));
            } else if (fabShadowYOffsetSeekBar.equals(seekBar)) {
                floatingActionButton.setShadowYOffset(getRealProgress(progress, seekBar.getMax()));
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            final StringBuilder toastTextBuilder = new StringBuilder("Shadow ");
            if (fabShadowXOffsetSeekBar.equals(seekBar)) {
                toastTextBuilder.append("X Offset = ");
            } else if (fabShadowYOffsetSeekBar.equals(seekBar)) {
                toastTextBuilder.append("Y Offset = ");
            }

            toastTextBuilder.append(getRealProgress(seekBar));

//            Snackbar.with(getActivity())
//                    .text(String.format("Fab Shadow X Offset:%s",
//                            getRealProgress(seekBar)))
//                    .duration(Snackbar.SnackbarDuration.LENGTH_SHORT)
//                    .show(getActivity());
        }

        private float getRealProgress(SeekBar seekBar) {
            return getRealProgress(seekBar.getProgress(), seekBar.getMax());
        }

        private float getRealProgress(int progress, int max) {
            return (float) (progress - max / 2) / SEEKBAR_PROGRESS_MULTIPLIER;
        }
    }

    private class ColorChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

        }

        private RadioButtons.ColorsInfo extractTag(RadioGroup group, int checkedId) {
            final RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
            return (RadioButtons.ColorsInfo) checkedRadioButton.getTag();
        }
    }
}
